<?php


namespace app\common\model\workflow;

use app\admin\model\ProjectFeeitem;
use app\common\model\apply\Advance;
use app\common\model\apply\Payment;
use app\common\model\apply\Reimbursement;
use app\common\model\Project;
use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;

class Workflow extends Model
{
    protected $name = 'wf_workflow';
    protected $json = ['content'];
    protected $jsonAssoc = true;
//    protected $append = ['project_name'];

    public static function list($user, $where, $alias, $limit, $order){
        $creator = Request::param('creator','','trim');
        $flowName = Request::param('flowName','','trim');
        $flowNo = Request::param('flowNo','','trim');
        $searchType = Request::param('searchType','','trim');

        if(!empty($name)){
            $where[] = ['workflow.creator_name','like','%'.$creator.'%'];
        }
        if(!empty($flowName)){
            $where[] = ['workflow.name','like','%'.$flowName.'%'];
        }
        if(!empty($flowNo)){
            $where[] = ['workflow.flow_no','=',$flowNo];
        }
        $query = Db::name('wf_workflow')->alias($alias);
        switch ($searchType){
            case 0:// 我发起的
                $where[] = ['workflow.creator_id','=',$user['id']];
                break;
            case 1://待我审核的
                $where[] = ['workflow.audit_id','find in set',$user['id']];
                $where[] = ['workflow.state','=', '1'];
                break;
            case 2://我已审批的
                $where[] = ['b.scope_id','=',$user['id']];
                $query->join('wf_node_record b','workflow.id = b.flow_id and b.record_type = 2 and b.node_type = 1')
                    ->group('workflow.id');
                break;
            case 3://抄送我的
                $where[] = ['b.scope_id','=',$user['id']];
                $query->join('wf_node_record b','workflow.id = b.flow_id and b.record_type = 1 and b.node_type = 2')
                    ->group('workflow.id');
                break;
        }

        $list = $query->withAttr('creator_time', function($value, $data) {
            return $value > 0 ? date('Y-m-d H:i:s',$value) : '';
        })->withAttr('update_time', function($value, $data) {
            return $value > 0 ? date('Y-m-d H:i:s',$value) : '';
        })->withAttr('complete', function($value, $data) {
            return $value > 0 ? date('Y-m-d H:i:s',$value) : '';
        })->withAttr('project_name', function($value, $data) {
            switch($data['flow_model']){
                case 1:
                    $applays = Reimbursement::where(['no' => $data['extend']])->with(['project'])->find();
                    break;
                case 2:
                    $applays =  Advance::where(['no' => $data['extend']])->with(['project'])->find();
                    break;
                case 3:
                    $applays =  Payment::where(['no' => $data['extend']])->with(['project'])->find();
                    break;
                case 4:
                    $applays =  Project::where(['id' => $data['extend']])->find();
                    $applays['project'] = $applays;
                    break;
            }
            return empty($applays['project']) || !isset($applays['project']['title']) ? "" : $applays['project']['title'];
        })->withAttr('now_node', function($value, $data) {
            return NodeRecord::where([
                'flow_id' => $data['id'],
                'node_step' => $data['audit_step'],
            ])->find();
        })->where($where)->order('workflow.update_time desc, workflow.id desc')->paginate($limit);

        return $list;
    }

    public static function getProjectNameAttr($value, $data){
        if(!empty($data['flow_model'])) return "";
        switch($data['flow_model']){
            case 1:
                $applays = Reimbursement::where(['no' => $data['extend']])->with(['project'])->find();
                break;
            case 2:
                $applays =  Advance::where(['no' => $data['extend']])->with(['project'])->find();
                break;
            case 3:
                $applays =  Payment::where(['no' => $data['extend']])->with(['project'])->find();
                break;
            case 4:
                $applays =  Project::where(['id' => $data['extend']])->find();
                $applays['project'] = $applays;
                break;
        }
        return $applays['project']['title'];
    }

    public function getCreatorTimeAttr($value)
    {
        return Date('Y-m-d H:i:s',$value);
    }

    public function nodes()
    {
        return $this->hasMany(Node::class,'flow_id');
    }

    public function records()
    {
        return $this->hasMany(NodeRecord::class,'flow_id');
    }

    public function scopes()
    {
        return $this->hasMany(NodeScope::class,'flow_id');
    }

    public static function onAfterDelete(Model $model): void
    {
        if(in_array($model->flow_model, [1,2,3])){
            $content = is_array($model->content) ? $model->content : json_decode($model->content, true);
            $amount = in_array($model->flow_model, [1,2]) ? $content['amount'] : $content['pay_amount'];
            ProjectFeeitem::where(['project_id' => $content['project_id'], 'feeitem_id' => $content['feeitem_id']])->save(['calculate' => 'calculate - '.$amount]);
        }
    }
}